package com.company;

public class ThreadYieldDemo {
    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(() -> {
            for(int x = 0; x < 100; x++) {
                if (x % 3 == 0) {
                    Thread.yield();
                    System.out.println(Thread.currentThread().getName() + "执行, yield");
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "执行, x=" + x);
            }
        }, "玩耍的线程");
        thread.start();
        for(int x = 0; x < 100; x++) {
            Thread.sleep(100);
            System.out.println("主线程执行， x=" + x);
        }
    }
}
